package com.xcmg.restaurant.restuarantservice.controller;

import com.xcmg.restaurant.apimodel.Restaurant;
import com.xcmg.restaurant.restuarantservice.feign.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * RestaurantController
 *
 * @author diyiliu
 * @create 2024-07-17 18:14
 **/

@RestController
public class RestaurantController {

    @Autowired
    private CustomerService customerService;

    private static Map<Long, Restaurant> mockRestaurantData = new HashMap();

    static {
        mockRestaurantData.put(1L, new Restaurant(1, "Pandas", "DC"));
        mockRestaurantData.put(2L, new Restaurant(2, "Indies", "SFO"));
        mockRestaurantData.put(3L, new Restaurant(3, "Little Italy", "DC"));
        mockRestaurantData.put(4L, new Restaurant(4, "Pizeeria", "NY"));
    }

    @GetMapping("/restaurant/customer/{id}")
    public List<Restaurant> getRestaurantForCustomer(@PathVariable("id") Long
                                                             id) {
        System.out.println("Got request for customer with id: " + id);

        String customerCity = customerService.getCustomerById(id).getCity();
        return mockRestaurantData.entrySet().stream().filter(
                        entry -> entry.getValue().getCity().equals(customerCity))
                .map(entry -> entry.getValue())
                .collect(Collectors.toList());
    }

}
